<?php

class ArticleTagModel
{
    private $autoUidModel;
    private $tagsKeyPrefix       =   'ATAG/';
    private $tagArticleKeyPrefix =   'TAGA/';
    
    public function __construct(){
        $this->autoUidModel         =   new AutoUidModel();
    }

    /**
     * 获取全部标签
     * @param $start
     * @param $length
     * @return array|bool
     */
    public function getAllTags($start, $length){
        $key       =   $this->tagsKeyPrefix. 0;
        $orderBy   = ' id DESC ';
    
        $result    =   Storage::getManyData($key, $orderBy, $start, $length);
        return $result;
    }

    /**
     * 通过标签搜索文章
     * @param $tag
     * @param $start
     * @param $length
     * @param $id
     * @param int $tagId
     * @return array|bool
     */
    public function searchArticleByTagName($tag, $start, $length, $id, $tagId = 0){
        if (empty($tagId)){
            $tagId = $this->getTagIdByName($tag);
            if (empty($tagId)) {
                return array();
            } 
        }
    	
    	$key        = $this->tagArticleKeyPrefix. $tagId;
    	$where      = " tag_id = ".$tagId;
    	// 分页
    	if ($id > 0){
    	    $start = 0;
    	    $where = " id < ".$id." AND tag_id = ".$tagId;
    	}
    	$orderBy    = ' id DESC ';
    	$result     =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);

    	return $result;
    }

    /**
     * @param $articleBodyId
     * @param $tagStr
     * @param $type
     * @return bool
     */
    public function editArticleTags($articleBodyId, $tagStr, $type = AC::TAG_TYPE_ARTICLE){
        if ($articleBodyId < 1){
            return FALSE;
        }

        // 判断标签字段是否设置值
        if (!empty($tagStr)){
        	$requireTags = array_slice(explode(',', $tagStr), 0, 5);
        }
    	// 获得用户所有的tags
    	$articleTags   = $this->getArticleAllTags($articleBodyId, $type);

    	if (!empty($articleTags)){
    	    // 删除文章的所有tags
    	    $this->delTagsByHashUid($articleBodyId, $type);
    	}
    	// 重新写入
        $insertTags = array();

    	if ($requireTags){
    	    foreach ($requireTags as $val){
    	        $tag = trim($val);
    	        if (empty($tag)){
    	            continue;
    	        }
                $tagId = $this->getTagIdByName($tag, true);

    	        if ($tagId){
                    $this->addTagByHashAid($articleBodyId, $tagId);
    	        }
                $insertTags[] = $tag;
    	    }
    	}
        $tagString = implode(',', $insertTags);
    	return $tagString;
    }

    /**
     * 获取文章全部标签
     * @param $typeId
     * @param $type
     * @return array|bool
     */
    public function getArticleAllTags($typeId, $type){
        if ($typeId < 1 || $type < 1){
            return FALSE;
        }

        $key       =   $this->tagArticleKeyPrefix. $typeId;
        $where     =   array('type_id' => $typeId, 'type' => $type, 'status' => 0);
        $result =   Storage::getConditionManyData($key, $where);
        return $result;
    }

    /**
     * 添加标签
     * @param $typeId
     * @param $tagId
     * @return bool|int|resource
     */
    private function addTagByHashAid($typeId, $tagId){
        if ($typeId < 1 || $tagId < 1){
            return FALSE;
        }
        
        $key       =   $this->tagArticleKeyPrefix. $typeId;
        $data      =   array(
            'tag_id'    =>$tagId,
            'type'      => AC::TAG_TYPE_ARTICLE,
            'created_at' => AC::SERVER_TIME,
            'updated_at' => AC::SERVER_TIME
            );
        $result    =   Storage::addData($key, $data);
        return $result;
    }


    /**
     * 删除所有标签
     * @param $typeId
     * @param $type
     * @return bool|int|resource
     */
    private function delTagsByHashUid($typeId, $type){
        if ($typeId < 1 || $type < 1){
            return FALSE;
        }
        
        $key       =   $this->tagArticleKeyPrefix. $typeId;
        $where     =   array('type_id' => $typeId, 'type' => $type, 'status' => 0);
        $data      =   array(
            'status' => 1,
            'updated_at' => AC::SERVER_TIME
            );
        $result    =   Storage::setConditionData($key, $data, $where);
        return $result;
    }

    /**
     * 根据名称获得标签id
     * @param $name
     * @param bool $need_insert
     * @return bool|int|resource
     */
    private function getTagIdByName($name, $need_insert = FALSE){
        if (strlen($name) < 1){
            return FALSE;
        }
        
    	$key    = $this->tagsKeyPrefix. 0;
    	$where  = array('name' => $name);
        $result = Storage::getConditionData($key, $where);

        $id     = 0;
        if ($result) {
            $id = $result['id'];
        }
        // 写入数据库
        if (empty($id) && $need_insert){
            $data = array(
                'name' => $name,
                'uid' => '111',
                'created_at' => AC::SERVER_TIME,
                'updated_at' => AC::SERVER_TIME,
                );
            $id = Storage::addData($key, $data);
        }
    	
    	return $id;
    }

}